package cn.wolfcode.luowowo.mgrsite.web.listener;

import cn.wolfcode.luowowo.acticle.domain.Answer;
import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.service.IAnswerService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.key.RedisKeys;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyRedisService;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyRedisService strategyRedisService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IAnswerService answerService;
    @Reference
    private IAnswerRedisService answerRedisService;
    @Reference
    private IUserInfoService userInfoService;

    //容器启动后  立刻执行
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

//        System.err.println("----data init begin---");
        //获取所有的攻略对象
        List<StrategyDetail> details = strategyDetailService.list();
        List<Answer> answers = answerService.list();
        List<UserInfo> userInfos = userInfoService.listUser();
        //details.forEach(System.err::println);
//        StrategyStatisVO vo = null;
//        for (StrategyDetail detail : details) {
            //判断redis中是否存在vo,存在的话跳过
//            if (strategyRedisService.isVOExist(detail.getId())) {
//                System.out.println(detail);
//                continue;
//            }
            //不存在的话拷贝其属性
//            vo = new StrategyStatisVO();
//            BeanUtil.copyProperties(vo, detail);
//            vo.setStrategyId(detail.getId());
//            vo.setDestId(detail.getDest().getId());
//            vo.setDestName(detail.getDest().getName());
//
//            strategyRedisService.setStatisVo(vo);
//
//        }
//        System.err.println("----data init end---");

       /* System.err.println("----zset_data init begin---");
        //国内外排行数据初始化
        for (StrategyDetail detail : details) {
            //判断redis中的对应zset里是否已经存在当前的value,在的话不操作
            if (strategyRedisService.isZsetVOExist(RedisKeys.STRATEGY_STATIS_SORT.getPrefix(), RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()))) {
                continue;
            }
            //点赞数+收藏数在前10名
            strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_SORT.getPrefix(),
                    RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()), detail.getThumbsupnum() + detail.getFavornum());
        }

        System.err.println("----zset_data init end---");

        System.err.println("----zset_hot init begin---");
        for (StrategyDetail detail : details) {
            //判断redis中对应zset里是否已经存在当前的value
            if (strategyRedisService.isZsetVOExist(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(), RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()))) {
                continue;
            }
            //浏览数+评论数前10名
            strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(),
                    RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()),detail.getReplynum()+detail.getViewnum());
        }
        System.err.println("----zset_hot init end---");*/


        System.err.println("----zset_amswerTup init begin---");
        for (Answer answer : answers) {
            //判断redis中对应zset里是否已经存在当前的value
            if (answerRedisService.isZsetVOExist(RedisKeys.ANSWER_STATIS_THUMB_USER.getPrefix(), RedisKeys.ANSWER_STATIS_VO.join(answer.getId().toString()))) {
                continue;
            }

            answerRedisService.addScore(RedisKeys.ANSWER_STATIS_THUMB_USER.getPrefix(),
                    RedisKeys.ANSWER_STATIS_VO.join(answer.getId().toString()),answer.getThumbupnum());
        }
        System.err.println("----zset_amswerTup init end---");

        System.err.println("----zset_UserComment init begin---");
        for (UserInfo userInfo : userInfos) {
            //判断redis中对应zset里是否已经存在当前的value
            if (answerRedisService.isZsetVOExist(RedisKeys.USER_STATIS_VO_COMMENT_USER.getPrefix(), RedisKeys.USER_STATIS_VO_COMMENT.join(userInfo.getId().toString()))) {
                continue;
            }

            answerRedisService.addScore(RedisKeys.USER_STATIS_VO_COMMENT_USER.getPrefix(),
                    RedisKeys.USER_STATIS_VO_COMMENT.join(userInfo.getId().toString()),userInfo.getCommentnum());
        }

        System.err.println("----zset_UserComment init end---");

    }
}
